package com.xh3140.xwidget.app.sliderbar;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.annotation.Nullable;

public class SliderBar extends AbstractSliderBar {
  // 值，必须在范围下限与上限之间
  private float mValue;

  // 激活状态，当用户按下滑块时，处于激活状态
  private boolean mActivated;

  // 触摸事件
  @Nullable
  private MotionEvent mMotionEvent;

  // 进度改变监听器
  @Nullable
  private OnValueChangedListener mOnValueChangedListener;

  public SliderBar(Context context) {
    super(context);
  }

  public SliderBar(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
  }

  public SliderBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  public SliderBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    // 滑块坐标
    final float blockX = mSliderStripRectF.left + v2d(mValue - mRangeLower);
    // 绘制滑条(普通状态)
    mPaint.setColor(mSliderStripColor);
    canvas.save();
    canvas.clipRect(blockX, mSliderStripRectF.top, mSliderStripRectF.right, mSliderStripRectF.bottom);
    if (mSliderStripRadius > 0) {
      canvas.drawRoundRect(mSliderStripRectF, mSliderStripRadius, mSliderStripRadius, mPaint);
    } else {
      canvas.drawRect(mSliderStripRectF, mPaint);
    }
    canvas.restore();
    // 绘制滑条(覆盖状态)
    mPaint.setColor(mSliderBlockColor);
    canvas.save();
    canvas.clipRect(mSliderStripRectF.left, mSliderStripRectF.top, blockX, mSliderStripRectF.bottom);
    if (mSliderStripRadius > 0) {
      canvas.drawRoundRect(mSliderStripRectF, mSliderStripRadius, mSliderStripRadius, mPaint);
    } else {
      canvas.drawRect(mSliderStripRectF, mPaint);
    }
    canvas.restore();
    // 绘制滑块
    mPaint.setColor(mSliderBlockColor);
    if (mActivated) {
      canvas.drawCircle(blockX, mSliderBlockCenter, 1.5f * mSliderBlockRadius, mPaint);
    } else {
      canvas.drawCircle(blockX, mSliderBlockCenter, mSliderBlockRadius, mPaint);
    }
  }

  @Override
  public boolean performClick() {
    return super.performClick();
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        mActivated = true;
        final float distance = event.getX() - mSliderStripRectF.left;
        setValue(constraintInRange(mRangeLower + d2v(distance)));
        mMotionEvent = MotionEvent.obtain(event);
        invalidate();
        break;
      case MotionEvent.ACTION_MOVE:
        if (mMotionEvent != null) {
          final float distance1 = event.getX() - mMotionEvent.getX();
          if (distance1 == 0F) return false;
          if (distance1 < 0F && mValue <= mRangeLower) return false;
          if (distance1 > 0F && mValue >= mRangeUpper) return false;
          setValue(constraintInRange(mValue + d2v(distance1)));
          mMotionEvent = MotionEvent.obtain(event);
          invalidate();
        }
        break;
      case MotionEvent.ACTION_UP:
        performClick();
        mActivated = false;
        if (mMotionEvent != null) {
          mMotionEvent.recycle();
          mMotionEvent = null;
        }
        invalidate();
        break;
    }
    return true;
  }

  /**
   * 获取值
   */
  public float getValue() {
    return mValue;
  }

  /**
   * 设置值
   *
   * @param value 值
   */
  public void setValue(float value) {
    if (mValue != value && mRangeLower <= value && mRangeUpper >= value) {
      mValue = value;
      if (mOnValueChangedListener != null) {
        mOnValueChangedListener.onValueChanged(mValue);
      }
      invalidate();
    }
  }

  /**
   * 设置值，悄悄的，不会引发监听器
   *
   * @param value 值
   */
  public void setValueQuietly(float value) {
    if (mValue != value && mRangeLower <= value && mRangeUpper >= value) {
      mValue = value;
      invalidate();
    }
  }

  /**
   * 设置值改变监听器
   *
   * @param listener 监听器
   */
  public void setOnValueChangedListener(@Nullable OnValueChangedListener listener) {
    mOnValueChangedListener = listener;
  }

  public interface OnValueChangedListener {
    void onValueChanged(float value);
  }
}
